<?php
/**
 * Created by PhpStorm.
 * User: hui
 * Date: 2016/9/2
 * Time: 9:43
 */

namespace Portal\Controller;
use Common\Controller\HomebaseController;

class ExamOnlineController extends HomebaseController
{
    function index(){
        $dispatch = M('dispatch');
        $ret = $dispatch->where(array('dispatch_id'=>I('uid'),'token'=>I('token')))->field('is_test')->find();
        if($ret){
            session("user",array(
                'uid'=>I('uid'),
                'token'=>I('token')
            ));
            if($ret['is_test']==2){
                $this->exam_pass();
            }else{
                $this->display();
            }
        }else{
            exit("<center style='padding:20px'>非法访问！</center>");
        }

    }
    function answer(){
        $user = session("user");
        $dispatch = M('dispatch');
        $dispatch-> where(array('dispatch_id'=>$user['uid']))->save(array('is_test'=>1));
            $test_database = M('test_database');
            $ret = $test_database->select();
            if($ret){
                $test = array_rand($ret,20);
                for($i = 0;$i < count($test);$i++){
                    $result[$i] = $ret[$test[$i]];
                    $answer[$i] = $ret[$test[$i]]['testdb_right_answer'];
                }
                $this->assign('result', $result);
                $this->assign('answer', $answer);
                $this->display();
            }

    }
    function answer_result(){
        $score = (int)I("score");
        $user = session("user");
        $result = array(
            "score"=>$score,
            "wrong"=>20-$score,
            "uid"=>$user['uid'],
            "token"=>$user['token']
        );
        $this->assign('result', $result);
        $this->display();
    }

    function exam_pass(){
        $user = session("user");
        $dispatch = M('dispatch');
        $dispatch-> where(array('dispatch_id'=>$user['uid']))->save(array('is_test'=>2));
        $this->display();
    }
    
}